package com.ruoyi.wisdom.event;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.wisdom.constants.IotConstants;
import com.ruoyi.wisdom.domain.*;
import com.ruoyi.wisdom.domain.method.PushTemplateMessagData;
import com.ruoyi.wisdom.domain.method.PushTemplateMessage;
import com.ruoyi.wisdom.domain.other.OtherOptions;
import com.ruoyi.wisdom.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class MessagePushEvent extends IotEvent {

    @Autowired
    private IIotPickSceneService iotPickSceneService;


    @Autowired
    private IIotPickRecordService iotPickRecordService;

    @Autowired
    private IIotStudentInfoService iotStudentInfoService;

    @Autowired
    private IIotStudentParentService iotStudentParentService;


    @Autowired
    private IIotSchoolHeadteacherService iotSchoolHeadteacherService;

    @Autowired
    private IIotSchoolClassesRelationService iotSchoolClassesRelationService;

    /**
     * 出去记录微信消息推送
     */
    public void pushPickRecordMessage(IotPickRecord row) {
        // 查询场景
        if (row.getSceneId() > 0) {
            IotPickScene sceneInfo = iotPickSceneService.selectIotPickSceneById(row.getSceneId().longValue());
            if (sceneInfo != null) {
                if (row.getPersonType() == IotConstants.USER_TYPE_TEACHER) {
                    pushPickRecordMessageToTeacher(row, sceneInfo);
                } else if (row.getPersonType() == IotConstants.USER_TYPE_STUDENT) {
                    pushPickRecordMessageToStudent(row, sceneInfo);
                }
            }
        }
    }

    /**
     * 教师消息推送
     *
     * @param row
     * @param sceneInfo
     */
    public void pushPickRecordMessageToTeacher(IotPickRecord row, IotPickScene sceneInfo) {

    }

    /**
     * 学生消息推送
     *
     * @param row
     * @param sceneInfo
     */
    public void pushPickRecordMessageToStudent(IotPickRecord row, IotPickScene sceneInfo) {
        // 微信消息推送demo
        // 1 入  2出
        // 应用场景 1 出入校门 2出入宿舍 3校门进出通用
        String content = "";
        if (row.getType() == IotConstants.TYPE_COME) {
            if (sceneInfo.getType() == 2) {
                content = "回到宿舍";
            } else {
                content = "回到学校";
            }
        } else if (row.getType() == IotConstants.TYPE_OUT) {
            if (sceneInfo.getType() == 2) {
                content = "离开宿舍";
            } else {
                content = "离开学校";
            }
        }

        PushTemplateMessagData data = new PushTemplateMessagData();
        data.setComId(row.getComId());
        data.setContent(content);
        data.setMessageType("track");
        data.setStudentName(row.getName());
        data.setCreateTime(row.getFaceTime());
        PushTemplateMessage message = new PushTemplateMessage();
        message.setData(data);
        // 推送给家长
        pushForParent(row, sceneInfo, message);
        // 推送给班主任
        pushForHeadteacher(row, sceneInfo, message);
        // 推送给值日老师
        pushForDutyTeacher(row, sceneInfo, message);

        // 更新记录为已经推送
        row.setIsPush(IotConstants.RECORD_PUSH_YES);
        iotPickRecordService.updateIotPickRecord(row);
    }

    /**
     * 推送给家长
     */
    private void pushForParent(IotPickRecord row, IotPickScene sceneInfo, PushTemplateMessage message) {
        if ((row.getType() == IotConstants.TYPE_COME && sceneInfo.getComeSendParent() > 0) ||
                (row.getType() == IotConstants.TYPE_OUT && sceneInfo.getLeaveSendParent() > 0)) {
            PushTemplateMessagData data = message.getData();
            data.setUserType(IotConstants.USER_TYPE_PARENT);

            // 查询家长
            IotStudentParent parentParams = new IotStudentParent();
            parentParams.setStudentId(row.getUuid());
            List<IotStudentParent> studentParents = iotStudentParentService.selectIotStudentParentList(parentParams);
            for (IotStudentParent studentParent : studentParents) {
                data.setTouser(studentParent.getParentUid());
                message.setData(data);
                System.out.println("推送微信消息："+JSONObject.toJSONString(message));
                redisSecondTemplate.opsForList().rightPush(IotConstants.PUSH_TEMPLATE_MESSAGE, JSONObject.toJSONString(message));
            }
        }
    }

    /**
     * 推送给班主任
     */
    private void pushForHeadteacher(IotPickRecord row, IotPickScene sceneInfo, PushTemplateMessage message) {
        if ((row.getType() == IotConstants.TYPE_COME && sceneInfo.getComeSendTeacher() > 0) ||
                (row.getType() == IotConstants.TYPE_OUT && sceneInfo.getLeaveSendTeacher() > 0)) {
            // 查询学生资料
            IotStudentInfo studentInfo = iotStudentInfoService.selectIotStudentInfoById(row.getUuid());
            if (studentInfo != null) {
                // 查询年级班级
                IotSchoolClassesRelation relationInfo = iotSchoolClassesRelationService.selectIotSchoolClassesRelationById(studentInfo.getRelationId());
                if (relationInfo != null) {
                    // 根据年级班级查询班主任
                    IotSchoolHeadteacher headteacherParams = new IotSchoolHeadteacher();
                    headteacherParams.setGradeId(relationInfo.getGradeId());
                    headteacherParams.setClassId(relationInfo.getClassId());
                    List<IotSchoolHeadteacher> headteachers = iotSchoolHeadteacherService.selectIotSchoolHeadteacherList(headteacherParams);
                    if (headteachers.size() > 0) {

                        PushTemplateMessagData data = message.getData();
                        data.setUserType(IotConstants.USER_TYPE_TEACHER);
                        for (IotSchoolHeadteacher headteacher : headteachers) {
                            data.setTouser(headteacher.getTeacherId());
                            message.setData(data);
                            redisSecondTemplate.opsForList().rightPush(IotConstants.PUSH_TEMPLATE_MESSAGE, JSONObject.toJSONString(message));
                        }
                    }
                }
            }
        }
    }

    /**
     * 推送给值日老师
     */
    private void pushForDutyTeacher(IotPickRecord row, IotPickScene sceneInfo, PushTemplateMessage message) {
        if (sceneInfo.getOptions() != null) {
            OtherOptions options = JSON.parseObject(sceneInfo.getOptions(), OtherOptions.class);
            if (((options.getDutyTeacher().getComeSendTeacher() > 0 && row.getType() == IotConstants.TYPE_COME)
                    || (options.getDutyTeacher().getLeaveSendTeacher() > 0 && row.getType() == IotConstants.TYPE_COME))
            && options.getDutyTeacher().getDutyUid() != null) {
                List<String> uids = Arrays.asList(options.getDutyTeacher().getDutyUid().split(","));
                PushTemplateMessagData data = message.getData();
                data.setUserType(IotConstants.USER_TYPE_TEACHER);
                for (String uid : uids) {
                    data.setTouser(Integer.parseInt(uid));
                    message.setData(data);
                    redisSecondTemplate.opsForList().rightPush(IotConstants.PUSH_TEMPLATE_MESSAGE, JSONObject.toJSONString(message));
                }
            }
        }
    }
}
